package com.century.sjt.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.entity.AboutSJT;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;
import com.pgyersdk.javabean.AppBean;
import com.pgyersdk.update.PgyUpdateManager;
import com.pgyersdk.update.UpdateManagerListener;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Description: 关于世纪通
 * @date : 2016/8/16 20:02
 */
public class AboutSJTActivity extends BaseActivity{
    @BindView(R.id.about_sjt_title)
    SjtTitleBar title;
    @BindView(R.id.about_sjt_jj)
    LinearLayout layoutJJ;
    @BindView(R.id.about_sjt_gnsh)
    LinearLayout layoutSM;
    @BindView(R.id.about_sjt_khfu)
    LinearLayout layoutFW;
    @BindView(R.id.about_sjt_vcode)
    LinearLayout layoutCode;
    @BindView(R.id.about_sjt_tvcode)
    TextView tvCode;

    private RequestQueue mQueue;
    private AboutSJT mAboutSJT = new AboutSJT();
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_about_activity);
        ButterKnife.bind(this);
        mQueue = Volley.newRequestQueue(this);
        mContext = this;
        initView();
        initEvent();
    }

    @Override
    protected void initView() {
        title.setTitle(getResources().getString(R.string.s_aboutsjt));
        title.setLeftIconResource(getResources().getString(R.string.icon_return));
        title.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initEvent() {
        //四季通简介
        layoutJJ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(AboutSJTActivity.this, AboutSJTJJActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("aboutSJT", mAboutSJT);
                intent.putExtras(mBundle);
                startActivity(intent);
            }
        });

        layoutFW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();

                intent.setClass(AboutSJTActivity.this, AboutSJTFWActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("aboutSJT", mAboutSJT);
                intent.putExtras(mBundle);
                startActivity(intent);
            }
        });

        layoutCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                PgyUpdateManager.register(AboutSJTActivity.this);
                PgyUpdateManager.register(AboutSJTActivity.this,
                        new UpdateManagerListener() {

                            @Override
                            public void onUpdateAvailable(final String result) {

                                // 将新版本信息封装到AppBean中
                                final AppBean appBean = getAppBeanFromString(result);
                                String versionCode = appBean.getVersionCode();
                                String versionName = appBean.getVersionName();
                                final String url = appBean.getDownloadURL();
                                String releaseNote = appBean.getReleaseNote();
                                AlertDialog alertDialog = new AlertDialog.Builder(AboutSJTActivity.this)
                                        .setCancelable(false)
                                        .show();
                                Window window = alertDialog.getWindow();
                                window.setContentView(R.layout.sjt_update_alertdialog);
                                TextView tv_title = (TextView) window.findViewById(R.id.tv_dialog_title);
                                tv_title.setText(getResources().getString(R.string.wing_gpdate));
                                TextView tv_message = (TextView) window.findViewById(R.id.tv_dialog_message);
                                tv_message.setText(releaseNote);
                                Button btn_submit = (Button) window.findViewById(R.id.btn_dialog_submit);
                                btn_submit.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        startDownloadTask(AboutSJTActivity.this, url);
                                    }
                                });

                            }

                            @Override
                            public void onNoUpdateAvailable() {
                                TipUtil.showToast(getResources().getString(R.string.error_nonew_update), AboutSJTActivity.this, 1);
                            }
                        });
            }
        });
        tvCode.setText(getVersion());

    }
    /**
     * 获取关于世纪通的信息
     */
    private void getAboutSJT() {
        //显示Progress Dialog
        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), AboutSJTActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.GetAboutSJT, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    TipUtil.closeProgressDialog();
                    TipUtil.log("接口成功返回", response.toString(), 1);
                    JSONTokener jsonParser = new JSONTokener(response);
                    JSONObject res = (JSONObject) jsonParser.nextValue();
                    String code = res.getString("code");
                    String message = res.getString("message");
                    JSONObject Data = res.getJSONObject("data");
                    if (code.equals("200")) {//成功
                        mAboutSJT.setContent(Data.getString("content"));
                        mAboutSJT.setAddress(Data.getString("address"));
                        mAboutSJT.setTelephone(Data.getString("telephone"));
                        mAboutSJT.setImage(Data.getString("image"));

                    } else {//失败
                        TipUtil.showToast(message, AboutSJTActivity.this, 1);
                    }
                } catch (JSONException ex) {
                    ex.printStackTrace();
                    TipUtil.showToast(getResources().getString(R.string.error_service), AboutSJTActivity.this, 1);
                }

                TipUtil.closeProgressDialog();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), AboutSJTActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        });
        mQueue.add(stringRequest);
    }
    @Override
    protected void onResume() {
        super.onResume();
        getAboutSJT();
    }
    /**
     * 2  * 获取版本号
     * 3  * @return 当前应用的版本号
     * 4
     */
    public String getVersion() {
        String version = "";
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            version = info.versionName;
            return "" + version;
        } catch (Exception e) {
            e.printStackTrace();
            return version;
        }
    }

    @Override
    protected void initData() {

    }
}
